package com.jmpt.fangbohui.ui.personalcenter;

import java.util.List;

import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.LoginBean;
import com.jmpt.fangbohui.dao.LoginSQLiteOperator;
import com.jmpt.fangbohui.ui.base.BaseActivity;
import com.jmpt.fangbohui.utils.DebugLog;
import com.jmpt.fangbohui.utils.SharePerfencesUtils;
import com.jmpt.fangbohui.utils.ToastHelper;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
/**
 * 
 *
 * @package com.jmpt.fangbohui.ui.personalcenter
 * @author cangJie
 * @description 登录经纪人界面
 *
 */
public class BrokerLoginActivity extends BaseActivity implements OnClickListener{

	private EditText et_activity_my_login_username;
	private EditText et_activity_my_login_password;
	private CheckBox cb_activity_my_login_remenber;
	private TextView tv_activity_my_register;
	private Button bt_activity_my_login;
	private ImageButton btn_base_head_left_imgbutton;
	private String username;
	private String password;
	
	private final static int SUCCESS = 1;
	private final static int ERROR = 2;
	private final static int FAILURE = 3;
	private static final String TAG = "LoginActivity";
	private static final String FILE_NAME="saveBrokerNamePwd";
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case SUCCESS :
				//保存密码
				if(cb_activity_my_login_remenber.isChecked()){
					SharePerfencesUtils.setBrokerCache(BrokerLoginActivity.this, username, password);
				}else{
					SharePerfencesUtils.setBrokerCache(BrokerLoginActivity.this, "", "");
				}
				
				LoginSQLiteOperator oper = new LoginSQLiteOperator(mContext);
				LoginBean login = (LoginBean) msg.obj;
				oper.addBroker(login);
				finish();
			break;
			case ERROR :
				Toast.makeText(BrokerLoginActivity.this, "账号或密码错误", 0).show();
				break;
			case FAILURE :
				Toast.makeText(BrokerLoginActivity.this, "网络连接出错", 0).show();
				break;
			}
		};
	};
	private Button bt_activity_broker_login;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		initEvent();
	}

	private void initEvent() {
		bt_activity_my_login.setOnClickListener(this);
		tv_activity_my_register.setOnClickListener(this);
		btn_base_head_left_imgbutton.setOnClickListener(this);
	}

	private void initView() {
		setContentView(R.layout.activity_my_login);
		et_activity_my_login_username = (EditText) findViewById(R.id.et_activity_my_login_username);
		et_activity_my_login_password = (EditText) findViewById(R.id.et_activity_my_login_password);
		cb_activity_my_login_remenber = (CheckBox) findViewById(R.id.cb_activity_my_login_remenber);
		tv_activity_my_register = (TextView) findViewById(R.id.tv_activity_my_register);
		bt_activity_my_login = (Button) findViewById(R.id.bt_activity_my_login);
		bt_activity_broker_login = (Button) findViewById(R.id.bt_activity_broker_login);
		bt_activity_broker_login.setVisibility(View.INVISIBLE);
		//下划线
		tv_activity_my_register.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		
		btn_base_head_left_imgbutton = (ImageButton) findViewById(R.id.btn_base_head_left_imgbutton);
		TextView tv_base_head_title = (TextView) findViewById(R.id.tv_base_head_title);
		Button btn_base_head_right_button = (Button) findViewById(R.id.btn_base_head_right_button);
		tv_base_head_title.setText("经纪人登录");
		btn_base_head_left_imgbutton.setVisibility(View.VISIBLE);
		btn_base_head_right_button.setVisibility(View.GONE);
		
		List<String> brokerCache = SharePerfencesUtils.getBrokerCache(this);
		et_activity_my_login_username.setText(brokerCache.get(0));
		et_activity_my_login_password.setText(brokerCache.get(1));
		
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.bt_activity_my_login:
			username = et_activity_my_login_username.getText().toString().trim();
			password = et_activity_my_login_password.getText().toString().trim();
			
			
			if(TextUtils.isEmpty(username)){
				ToastHelper.ShowToast("账户不能为空", this);
				return;
			}
			if(TextUtils.isEmpty(password)){
				ToastHelper.ShowToast("密码不能为空", this);
				return;
			}
				checkUser(username,password);
		break;
		case R.id.btn_base_head_left_imgbutton:
			finish();
		break;
		case R.id.tv_activity_my_register:
			Log.d(TAG, "进入注册页面");
			Intent register = new Intent(this,BrokerRegisterActivity.class);
			startActivity(register);
		break;
		
		}
	}
	public void checkUser(String username,String password){
		final HttpUtils utils = new HttpUtils();
		String url = com.jmpt.fangbohui.Constants.URL.BROKERLOGINURL;
		RequestParams params = new RequestParams();
		params.addBodyParameter("phone", username);
		params.addBodyParameter("password", password);
		utils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
			Message msg = null;
			private String PHPSESSID;
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				LoginBean webLoginBean = null;
				String result = responseInfo.result;
				DebugLog.i("broker"+result);
//				int start = result.indexOf("{");
//				int end = result.indexOf("}") + 2;
//				result = result.substring(start, end);
				DebugLog.i(result);
				Gson gson = new Gson();
				webLoginBean = gson.fromJson(result, LoginBean.class);

				if(webLoginBean.done.equals("true")){
				
					webLoginBean.getRetval().setType(2);
					if(webLoginBean.getRetval().getFlag().equals("error")){
						msg = Message.obtain();
						msg.what = ERROR;
						handler.sendMessage(msg);
					}else{
						msg = Message.obtain();
						msg.what = SUCCESS;
						msg.obj = webLoginBean;
						handler.sendMessage(msg);
					}

				}else{
					msg = Message.obtain();
					msg.what = ERROR;
					handler.sendMessage(msg);
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				System.out.println("failure");
				this.msg = Message.obtain();
				this.msg.what = FAILURE;
				handler.sendMessage(this.msg);
			}
		});

	}


}
